const PHONE_DECODE_ENCODE_KEY = 'myd#AlO%$*&^1dwTRwallet';

const formatData = function (date, format) {
    if (typeof date === 'number') {
        date = new Date(date);
    }
    var o = {
        'M+': date.getMonth() + 1, //month
        'd+': date.getDate(), //day
        'h+': date.getHours(), //hour
        'm+': date.getMinutes(), //minute
        's+': date.getSeconds(), //second
        'q+': Math.floor((date.getMonth() + 3) / 3), //quarter
        'S': date.getMilliseconds() //millisecond
    };

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp('(' + k + ')').test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length === 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length));
        }
    }
    return format;
};

const formatDateToSec = (timestamp) => {
    if (isEmpty(timestamp)) {
        return;
    }
    return formatData(timestamp, 'yyyy-MM-dd hh:mm:ss');
};

const isPhone = (value) => {
    if (isEmpty(value)) {
        return;
    }
    value = value.toString();
    value = value.replace(/\s/g, "");
    return value && value.length === 11 && (/^1\d{10}$/g).test(value);
};

const isEmail = value => {
    if (isEmpty(value)) {
        return false;
    }
    const pattern = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    value = value.toString();
    value = value.replace(/\s/g, "");
    return value && pattern.test(value);
}

const delSpace = (value) => {
    return value.toString().replace(/\s/g, "");
};

const isNumeric = (value) => {
    var reg = /^\d$/;
    return value && reg.test(value);
};

const isMsgCode = (value) => {
    var reg = /^\d{6}$/;
    return value && reg.test(value);
};

//去重
const unique = (arr) => {
    var result = []
    for (var i = 0; i < arr.length; i++) {
        if (result.indexOf(arr[i]) == -1) {
            result.push(arr[i])
        }
    }
    return result;
};

//从大到小 排序
const sort = (result) => {
    var arr = result;
    for (var i = 0; i < arr.length; i++) {
        for (var j = i + 1; j < arr.length; j++) {
            if (arr[i] < arr[j]) {
                var tmp = arr[i];
                arr[i] = arr[j];
                arr[j] = tmp;
            }
        }
    }
    return arr;
};

const formatPhoneNumber = (number) => {
    if (isEmpty(number)) {
        return number;
    }
    number = number.toString();
    number = number.replace(/\s/g, "");
    number = number.replace(" ", "").replace("-", "").replace("—", "");
    if (number.indexOf("+86") == 0) {
        number = number.substring(3);
    }
    if (number.length > 11) {
        return number.substring(number.length - 11, number.length);
    }
    return number;
};

const isEmpty = (val) => {
    if (val === null || val === '' || typeof val === 'undefined') {
        return true;
    }
    return false;
};

const enCodeMobile = (number) => {
    if (isEmpty(number)){
        return;
    }

    number = number.toString();
    if (!isPhone(number)){
        return;
    }

    return number.substring(0, 3) + "****" + number.substring(7, 11);
};

const formatPhone = (value) => {
    if (isEmpty(value)) {
        return;
    }
    value = value.toString();
    if (!isPhone(value)) {
        return;
    }
    return  value.substring(0, 3) + " " + value.substring(3, 7) + " " + value.substring(7, 11);
};

const formatCardNumber = (value) => {
    if (isEmpty(value)) {
        return '';
    }
    value = value.toString();
    var tempValue = '';
    for (var i = 0; i < value.length; i = i+3){
        tempValue += value.substr(i, 3) + ' ';
    }
    return  tempValue;
};

module.exports = {
    formatData,
    formatDateToSec,
    formatPhoneNumber,
    isEmpty,
    isPhone,
    isEmail,
    unique,
    isNumeric,
    isMsgCode,
    formatPhone,
    formatCardNumber,
    sort,
    delSpace,
    enCodeMobile,
    PHONE_DECODE_ENCODE_KEY,
};
